package dev.ece.suit.ui;

import org.apache.commons.lang3.StringUtils;

import dev.ece.suit.text.TextLibrary;
import dev.ece.suit.ui.utils.Alignment;
import dev.ece.util.lib.IntRectangle;
import dev.ece.util.lib.Rectangle;
import dev.ece.util.lib.Vector3f;
import dev.ece.util.lib.Vector4f;
import dev.ece.util.tools.Camera;

public class Label extends Container {
	
	/**
	 * 
	 */
	private static final long serialVersionUID = 7398176371845578234L;
	
	private String text;
	
	private int fontSize;
	
	private TextLibrary textLibrary = Fonts.getSystemTextLibrary();
	
	private Vector4f fontColor = new Vector4f(1, 1, 1, 1);
	
	private final Vector3f textPosition = new Vector3f();
	
	private int alignment = Alignment.LEFT;
	
	public Label(String text, float width, float height) {
		super(width, height);
		this.text = text;
		this.setupLabel();
	}

	protected void setupLabel() {
		this.fontSize = (int) (getHeight() * 2 / 3);
		this.computeTextPostion();
	}
	
	@Override
	protected void renderChildren(Camera camera) {
		renderText();
		super.renderChildren(camera);
	}

	protected void renderText() {
		if(StringUtils.isNotBlank(getText())) {
			getTextLibrary().renderSimpleText(getText(), getFontSize(), getFontColor(), getTextPosition());
		}
	}
	
	public int getFontSize() {
		return fontSize;
	}

	public void setFontSize(int fontSize) {
		if(fontSize <= 0) {
			throw new IllegalArgumentException("fontSize必须大于0");
		}
		if(textLibrary == null) {
			throw new IllegalArgumentException("textLibrary不能为null");
		}
		this.fontSize = fontSize;
		this.computeTextPostion();
	}

	public String getText() {
		return text;
	}

	public void setText(String text) {
		if(textLibrary == null) {
			throw new IllegalArgumentException("textLibrary不能为null");
		}
		this.text = text;
		this.computeTextPostion();
	}

	public int getAlignment() {
		return alignment;
	}

	public void setAlignment(int alignment) {
		this.alignment = alignment;
		this.computeTextPostion();
	}

	protected void computeTextPostion() {
		if(StringUtils.isNotBlank(getText())) {
			float textWidth = getTextLibrary().getCharsWidth(getText(), getFontSize());
			float textHeight = getTextLibrary().getLineHeight(getFontSize());
			Rectangle rect = getViewRectangle();
			getTextPosition().setY(textHeight / 2);
			switch(getAlignment()) {
				case Alignment.CENTER:
					getTextPosition().setX(-textWidth / 2);
					return;
				case Alignment.RIGHT:
					getTextPosition().setX(rect.right - textWidth);
					return;
				default:
					getTextPosition().setX(rect.left);
			}
		}
	}
	
	public Vector4f getFontColor() {
		return fontColor;
	}

	protected Vector3f getTextPosition() {
		return textPosition;
	}

	public void setFontColor(Vector4f fontColor) {
		if(fontColor == null) {
			throw new IllegalArgumentException("fontColor不能为null");
		}
		this.fontColor = fontColor;
	}

	public TextLibrary getTextLibrary() {
		return textLibrary;
	}

	public void setTextLibrary(TextLibrary textLibrary) {
		if(textLibrary == null) {
			throw new IllegalArgumentException("textLibrary不能为null");
		}
		this.textLibrary = textLibrary;
		this.computeTextPostion();
	}

	@Override
	public void setPadding(IntRectangle padding) {
		super.setPadding(padding);
		this.computeTextPostion();
	}

}
